package com.android.home.fragment;

import android.view.ViewGroup;
import com.android.common.mvp.model.CodeMsgBean;
import com.android.common.base.rv.BaseRvAdapter;
import com.android.common.base.rv.BaseRvFragment;
import com.android.common.base.rv.BaseRvHolder;
import com.android.common.utils.Router;
import com.android.home.BuildConfig;
import com.android.home.R;
import com.android.home.adapter.holder.HomeSecondHolder;
import com.android.home.mvp.model.home.HomeSecondBean;
import com.android.home.mvp.model.home.MultiSelectListBean;
import com.android.home.mvp.present.home.HomeSecondPresent;

public class HomeSecondFragment extends BaseRvFragment<HomeSecondBean.ResultBean> {

    public static HomeSecondFragment instance;

    private HomeSecondPresent present;

    private boolean mNeedBack = false;

    public static HomeSecondFragment getInstance(){
        if (null == instance){
            instance = new HomeSecondFragment();
        }

        return instance;
    }

    public HomeSecondFragment(){
    }

    @Override
    protected void onInitView() {
        super.onInitView();
        present = new HomeSecondPresent(this);
    }

    @Override
    protected boolean onNeedBack() {
        return mNeedBack;
    }

    @Override
    protected String onSetTitleText() {
        return getString(R.string.contact);
    }

    @Override
    protected BaseRvAdapter<HomeSecondBean.ResultBean> initAdapter(){
        BaseRvAdapter<HomeSecondBean.ResultBean> adapter = new BaseRvAdapter(this.getContext()){

            @Override
            protected BaseRvHolder<HomeSecondBean.ResultBean> createDataViewHolder(ViewGroup parent, int viewType) {
                return new HomeSecondHolder(mLayoutInflater.inflate(R.layout.item_home_second,parent,false));
            }
        };
        adapter.setOnItemClickListener((view,position) ->{
            Router.getInstance().startActivity(this.getContext(),null, BuildConfig.HOME_SECOND);
        });
        return adapter;
    }


    @Override
    public void onFailed(CodeMsgBean responseBean, String methodName) {
        super.onFailed(responseBean,methodName);
        refreshOrLoadFailed();
    }

    @Override
    protected void onLoadData() {
        super.onLoadData();
        showProgress();
        present.getHomeSecondList();
    }

    @Override
    public void onPullRefresh() {
        super.onPullRefresh();
        showProgress();
        present.getHomeSecondList();
    }

    @Override
    public void onPullLoadMore() {
        super.onPullLoadMore();
        showProgress();
        present.getHomeSecondList();
    }

    @Override
    public <D> void onSucceed(D d, String methodName) {
        super.onSucceed(d, methodName);
        if (d instanceof HomeSecondBean){
            HomeSecondBean bean = (HomeSecondBean)d;
            refreshOrLoadSuccess(bean.getResult(),bean.getTotal());
        }
    }

    public void setNeedBack(boolean needBack) {
        this.mNeedBack = needBack;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        instance = null;
    }
}
